<html>
<head><meta charset="utf-8"><title>Marking functions using custom attributes · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Marking.20functions.20using.20custom.20attributes.html">Marking functions using custom attributes</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="239945430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Marking%20functions%20using%20custom%20attributes/near/239945430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Marking.20functions.20using.20custom.20attributes.html#239945430">(May 23 2021 at 11:50)</a>:</h4>
<p>Hey, I'm working on a cargo tool for taint analysis, and I'm trying to figure out the best way to tag functions with annotations. I've had a look at the tool prelude, but the reference didn't have much to say about it. Is there a way to register an annotation in the tool prelude using compiler callbacks, or maybe some way of identifying all uses of a given attribute?</p>
<p>For example, I think the ideal would be to use the tool prelude to mark functions as sources and sinks:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[taintrs::source]</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">get_user_input</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="cp">#[taintrs::sink]</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">eval_sql</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>But would also be happy to use proc macros, as long as I had a way of tracking it either before expansion or in MIR. I've had a look at what MIRI does, which is sort of similar, but I'm not looking to implement a full abstract interpreter.</p>



<a name="239945471"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Marking%20functions%20using%20custom%20attributes/near/239945471" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Marking.20functions.20using.20custom.20attributes.html#239945471">(May 23 2021 at 11:51)</a>:</h4>
<p>I've also considered using a proc_macro_attribute to conditionally add an annotation from the tool prelude if the <code>taint</code> config option was set, similarly to how MIRI uses annotations, but I'm not at all sure about how to get started with the prelude, again. Ideally, I would like this to be something I could compile on top of any given Rust compiler, without having to build the entire toolchain from source.</p>



<a name="239967001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Marking%20functions%20using%20custom%20attributes/near/239967001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Marking.20functions.20using.20custom.20attributes.html#239967001">(May 23 2021 at 17:48)</a>:</h4>
<p>There is the unstable <code>#![register_tool]</code> ). You need to add something like <code>#![register_tool(taintrs)]</code> at the start of the crate root.</p>



<a name="239969292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Marking%20functions%20using%20custom%20attributes/near/239969292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Marking.20functions.20using.20custom.20attributes.html#239969292">(May 23 2021 at 18:26)</a>:</h4>
<p>The compiler should probably suggest that for unrecognized tools</p>



<a name="240009291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Marking%20functions%20using%20custom%20attributes/near/240009291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Marking.20functions.20using.20custom.20attributes.html#240009291">(May 24 2021 at 05:54)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span>  That works, thanks for the tip.</p>
<p>I'm still unsure about how to extract this information from the crate, e.g. given the code above, how to actually find all items that have been tagged with <code>#[taintrs::source]</code> and <code>#[taintrs::sink]</code> respectively. Whatever I can come up with seems a little hacky, guessing at the Id that the Attribute has been assigned per compilation...</p>



<a name="240015910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Marking%20functions%20using%20custom%20attributes/near/240015910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Marking.20functions.20using.20custom.20attributes.html#240015910">(May 24 2021 at 07:30)</a>:</h4>
<p>I think creating a hir vistor would work. This vistor can walk all items and check if any have been marked with those attributes.</p>



<a name="240016027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Marking%20functions%20using%20custom%20attributes/near/240016027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Marking.20functions.20using.20custom.20attributes.html#240016027">(May 24 2021 at 07:32)</a>:</h4>
<p>Awesome. Thanks. :D</p>



<a name="240019923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Marking%20functions%20using%20custom%20attributes/near/240019923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Marking.20functions.20using.20custom.20attributes.html#240019923">(May 24 2021 at 08:18)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> Would that be the <code>rustc_hir::intravisit::Visitor</code>?</p>



<a name="240020035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Marking%20functions%20using%20custom%20attributes/near/240020035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Marking.20functions.20using.20custom.20attributes.html#240020035">(May 24 2021 at 08:20)</a>:</h4>
<p>I think ItemLikeVisitor would be enough.</p>



<a name="240020133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Marking%20functions%20using%20custom%20attributes/near/240020133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Marking.20functions.20using.20custom.20attributes.html#240020133">(May 24 2021 at 08:20)</a>:</h4>
<p>Getting all items with a specific attribute is listed as one of the use cases in the docs of ItenLikeVisitor.</p>



<a name="240026716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Marking%20functions%20using%20custom%20attributes/near/240026716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hilmar Gústafsson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Marking.20functions.20using.20custom.20attributes.html#240026716">(May 24 2021 at 09:27)</a>:</h4>
<p>Awesome. I was able to get it to work. Thank you a lot <span aria-label="smiley" class="emoji emoji-1f603" role="img" title="smiley">:smiley:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>